window.onload = ()=>{
	var ul = document.querySelector('.operation');
	// 添加按钮
	var addLi = ul.firstElementChild || ul.firstChild;
	EventListener.addEvent(addLi, 'click', (e)=>window.location.href="/officalWebsite/admin/addSignUp.html");
	// 修改按钮
	var modiLi = ul.children[1];
	EventListener.addEvent(modiLi, 'click', ()=>alert('暂不提供此功能！'));
	// 批量删除
	var delsBtn = ul.children[2];
	EventListener.addEvent(delsBtn, 'click', function(){
		var ids = getCheckedBox();
		if(ids.length>0){
			var bool = confirm('是否确认删除已选中的数据?');
			if(bool){
				ajax({
					method : 'POST',
					url : '/officalWebsite/info/signUp?method=removeMutiple',
					async : true,
					data : {
						'ids' : ids
					},
					success : function(data){
						if(data=='true'){
							alert('删除成功！');
							searchSignUps(1);
						}else{
							alert('删除失败，请重试！');
						}
					}
				});
			}
		}else{
			alert('请选择要删除的数据！');
		}
	});
	
	// excel导出按钮
	var excelLi = ul.children[3];
	EventListener.addEvent(excelLi, 'click', function(){
		var ids = getCheckedBox();
		if(ids.length>0){
			var bool = confirm('是否导出已选数据？')
			var url = '/officalWebsite/info/signUp?method=exportInfoAsExcel&ids='+ids;
			window.location = url;
		}
		if(ids.length==0){
			var bool = confirm('是否导出全部数据？');
			if(bool){
				window.location = '/officalWebsite/info/signUp?method=exportAllInfoAsExcel';
			}
		}
	});
	
	// word导出按钮
	var wordLi = ul.children[4];
	EventListener.addEvent(wordLi, 'click', function(){
		var ids = getCheckedBox();
		if(ids.length==1){
			var bool = confirm('是否导出已选数据？')
			var url = '/officalWebsite/info/signUp?method=exportInfoAsWord&id='+ids[0];
			window.location = url;
		}else if(ids.length>1){
			alert('目前未能导出多条数据,请操作单条！');
		}else{
			alert('请选择需要导出的数据！');
		}
	});
	
	  // 分页查询
    (function(){
    	// 查询条件提交
        var searchBtn = document.querySelector('#search');
        searchBtn.addEventListener('click', function() {
        	var currentPage = document.querySelector('#current').innerHTML;
        	searchSignUps(currentPage);
        });
        // 首页
        var firstBtn = document.querySelector('#first');
        firstBtn.addEventListener('click', function() {
        	var currentPage = document.querySelector('#current').innerHTML;
        	currentPage = parseInt(currentPage)
        	if(currentPage==1){
        		return;
        	}else{
        		searchSignUps(1);
        	}
        });
        // 尾页
        var lastBtn = document.querySelector('#last');
        lastBtn.addEventListener('click', function() {
        	var currentPage = document.querySelector('#current').innerHTML;
        	currentPage = parseInt(currentPage)
        	var totalPage = document.querySelector('#total').innerHTML;
        	if(currentPage==totalPage){
        		return;
        	}else{
        		searchSignUps(totalPage);
        	}
        });
        // 上一页
        var preBtn = document.querySelector('#pre');
        preBtn.addEventListener('click', function(){
        	var currentPage = document.querySelector('#current').innerHTML;
        	currentPage = parseInt(currentPage)-1;
        	if(currentPage<=0){
        		alert('当前已是首页了！')
        	}else{
        		searchSignUps(currentPage);
        	}
        });
        // 下一页
        var nextBtn = document.querySelector('#next');
        nextBtn.addEventListener('click', function(){
        	var currentPage = document.querySelector('#current').innerHTML;
        	var totalPage = document.querySelector('#total').innerHTML;
        	currentPage = parseInt(currentPage)+1;
        	if(currentPage>totalPage){
        		alert('当前已是最后一页了！')
        	}else{
        		searchSignUps(currentPage);
        	}
        });
        // 指定页数跳转
        var goPageBtn = document.querySelector('.find');
        goPageBtn.addEventListener('click', function(){
        	var totalPage = document.querySelector('#total').innerHTML;
        	var pageValue = document.querySelector('#page').value;
        	if(pageValue<0 || pageValue>totalPage){
        		alert('请指定正确的页数！');
        	}else{
        		searchSignUps(pageValue);
        	}
        });
        // 初始化调用
        searchSignUps(1);
    })();
    
    function bindDynamicBtn(){
    	// 删除
        var allDelBtn = document.querySelectorAll('.deleteBtn');
        for(var i=0;i<allDelBtn.length;i++){
        	allDelBtn[i].addEventListener('click', function(e){
            	var bool = confirm('请确认是否删除！');
            	if(bool){
                	var tr = this.parentNode.parentNode;
                	var input = tr.children[1].lastElementChild || tr.children[1].lastChild;
                	ajax({
                		method : 'POST',
                		url : '/officalWebsite/info/signUp?method=removeSingle'+'&nowtime='+new Date().getTime(),
                		async : true,
                		data : {
                			'id' : input.value
                		},
                		success : function(data){
                			if(data){
                				alert('删除成功！');
                				searchSignUps(1);	// 刷新
                			}else{
                				alert('删除失败！');
                			}
                		}
                	});
            	}
            });
        }
        // 查看
        
    }
    
    function searchSignUps(currentPage){
    	var tbody = document.querySelector('#info');
    	var name = document.querySelector('#name').value;
    	var phone = document.querySelector('#phone').value;
    	var current = document.querySelector('#current');
    	var total = document.querySelector('#total');
    	var count = document.querySelector('#count');
    	// 移除原有子节点
		clearChilds();
		// 调用异步请求方法
		ajax({
	        method : 'POST',
	        url : '/officalWebsite/info/signUp?method=fySearch'+'&nowtime='+new Date().getTime(),
	        async : true,
	        data :
	        {
	        	'name': name,
	        	'phone': phone,
	        	'currentPage': currentPage
	        },
	        success : function(data){
	        	if(data!=="" && data!=null){
	        		data = JSON.parse(data);
		        	var list = data.signUps;
		        	for(var i=0;i<list.length;i++){
		        		var number = (data.currentPage-1)*data.pageSize+i+1;
		        		tbody.innerHTML += '<tr>'+
							                    '<td>'+
							                        '<input type="checkbox">'+
							                    '</td>'+
							                    '<td>'+number+'<input type="hidden" value="'+list[i].id+'" name="id"/></td>'+
							                    '<td>'+list[i].name+'</td>'+
							                    '<td>'+list[i].gender+'</td>'+
							                    '<td>'+list[i].classes+'</td>'+
							                    '<td>'+list[i].telephone+'</td>'+
							                    '<td>'+list[i].groupName+'</td>'+
							                    '<td>'+list[i].introduction+'</td>'+
							                    '<td>'+
							                        '<a href="javascript:;" target="_blank">查看</a>'+
							                        '<a href="javascript:;" class="deleteBtn">删除</a>'+
							                    '</td>'+
							               '</tr>';
		        	}
		        	current.innerHTML = data.currentPage;
		        	total.innerHTML = data.totalPage;
		        	count.innerHTML = data.count;
		        	bindDynamicBtn();		// 为动态添加的按钮绑定事件
		        	// 绑定全选事件
		        	bindSelectAll();
	        	}
	        },
	    });
    }
    
    function bindSelectAll(){
    	var boxs = document.querySelectorAll('input[type="checkbox"]');
    	var selectAll = boxs[0];
    	EventListener.addEvent(selectAll, 'click', function() {
    	    var bool = selectAll.checked;
    	    if (bool) {
    	        for (var i = 1; i < boxs.length; i++) {
    	            boxs[i].checked = bool;
    	        }
    	    } else {
    	        for (var i = 1; i < boxs.length; i++) {
    	            boxs[i].checked = bool;
    	        }    
    	    }
    	});
    }
    
    // 移除tbody的所用子节点
	function clearChilds(){
		var tbody = document.querySelector('#info');
		var trs = tbody.children;	
		// 因HTMLCollection会随着文档的改变而改变，要使用原来的可先将其保存到数组中
		var arr = [];
		for(var i=0;i<trs.length;i++){
			arr.push(trs[i]);
		}
		for(var i=0;i<arr.length;i++){
			tbody.removeChild(arr[i]);
		}
	}
}